import { Command } from '../constant';
import Taro, { getCurrentInstance } from '@tarojs/taro';
import { Dispatch } from 'typings';
import { IAllReducerProps } from '../types';
import { getReducerData } from '@/redux/store';
import { extraPathsValue } from '@/redux/util';
import api from 'api';

export default (dispatch: Dispatch) => {
  let action = {
    commonChange(...param: any) {
      dispatch({
        type: Command.commonChange,
        payload: extraPathsValue(...arguments),
      });
    },
    /**
     * 活动详情
     */
    async page() {
      let { id } = getCurrentInstance().router.params;
      let { user } = Taro.getStorageSync('b2b-wechat@login');
      let params = {
        id,
        registerBy: user?.id || ''
      }
      let res = await api.pagesIndexController.getActiveDetail(params);
      if (res.success) {
        action.commonChange('main.detail', res.data)
      }
    },
    /**
     * 报名
     */
    async addRegister() {
      let { detail } = getData().main;
      let { user } = Taro.getStorageSync('b2b-wechat@login');
      let params = {
        userId: user?.id || '',
        registerBy: user?.id || '',
        activityId: detail.id,
      }
      await api.pagesIndexController.addRegister(params);
      Taro.showToast({
        title: '报名成功！',
        icon: 'none',
        duration: 2000,
      });
      setTimeout(() => {
        Taro.navigateBack();
      }, 1000);
    }
  };
  return action;
};

function getData(): IAllReducerProps {
  return {
    main: getReducerData('packageCActiveDetailMain'),
  };
}

//create by moon https://github.com/creasy2010/moon
